package com.imooc.article.html;

import com.imooc.api.config.RabbitMQConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Project_name: imooc-news-dev
 * @Description:
 * @Author: Levi
 * @Date: 2021/3/29 19:44
 */
@Component
public class RabbitMQConsumer {

    @Autowired
    private ArticleHtmlComponent articleHtmlComponent;

    @RabbitListener(queues = RabbitMQConfig.QUEUE_DOWNLOAD_HTML)
    public void watchQueue(String payload, Message message) {

//        System.out.println(payload);
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();

        if ("article.download.do".equalsIgnoreCase(routingKey)) {
            String articleId = payload.split(",")[0];
            String mongoArticleId = payload.split(",")[1];
            try {
                articleHtmlComponent.download(articleId, mongoArticleId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("article.delete.do".equalsIgnoreCase(routingKey)) {
            articleHtmlComponent.deleteGridFSAndHtml(payload);
        }
    }
}
